import os
import sys
# 添加项目根目录到 Python 路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.database import SessionLocal
from app.models import User
from app.core.security import get_password_hash

def create_admin(username: str, password: str, email: str):
    db = SessionLocal()
    try:
        # 检查是否已存在
        existing_user = db.query(User).filter(User.username == username).first()
        if existing_user:
            print(f"用户名 {username} 已存在!")
            return

        admin = User(
            username=username,
            email=email,
            password=get_password_hash(password),
            role="admin"
        )
        db.add(admin)
        db.commit()
        print(f"管理员账号 {username} 创建成功!")
    except Exception as e:
        print(f"创建失败: {str(e)}")
        db.rollback()
    finally:
        db.close()

if __name__ == "__main__":
    username = input("请输入管理员用户名: ")
    password = input("请输入管理员密码: ")
    email = input("请输入管理员邮箱: ")
    create_admin(username, password, email) 